package com.example.mylibrarybase.yumengmeng25.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.example.mylibrarybase.R;
import com.example.mylibrarybase.bar.initAction_Bar;
import com.example.mylibrarybase.base.baseui.BaseActivity;
import com.example.mylibrarybase.utils.LogUtils;
import com.example.mylibrarybase.utils.UtilsToast;
import com.example.mylibrarybase.yumengmeng04.base.BaseListViewAdapter_04;
import com.example.mylibrarybase.yumengmeng25.adapter.mmyAdapager;
import com.example.mylibrarybase.yumengmeng25.bean.Che;
import com.example.mylibrarybase.yumengmeng25.utils.Constants;
import com.example.mylibrarybase.yumengmeng25.utils.NetAesCallBack;
import com.example.mylibrarybase.yumengmeng25.utils.NetMessage;

import java.util.Hashtable;
import java.util.List;

public class MainActivity_25_jsonjiamijiemi extends BaseActivity {

    private initAction_Bar mActivity_main_25_jsonjiamijiemi;
    private initAction_Bar base_action_bar;
    private TextView mTv_25show;
    private TextView mTv_25_getjson;
    private TextView tv_25showhandler;
    Handler mHandler= new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
//                    Bundle data = msg.getData();
//                    String aesString = data.getString("aesString");
//                    mTv_25_getjson.setText(aesString);
                break;
            }


            Bundle data = msg.getData();
            String sendPosition = data.getString("sendPosition");
            String aesString = data.getString("aesString");
            if (null!=sendPosition){

                mTv_25_getjson.setText(sendPosition);
            }
            if (null!=aesString){

                mTv_25_getjson.setText(aesString);
            }

            LogUtils.i("我收到消息了"+sendPosition);
        }
    };
    private TextView tv_clear;
    private ListView mLv_listview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    @Override
    public int getlayouXML() {
        return R.layout.activity_main_25_jsonjiamijiemi;
    }

    @Override
    public void initView() {
        base_action_bar = (initAction_Bar) findViewById(R.id.base_action_bar);
        base_action_bar.setCallBack(new initAction_Bar.Action_bar_call_back() {
            @Override
            public void getAction_barView_backbutton(TextView textView) {

            }

            @Override
            public void getAction_barView_backbutton_icon(ImageView imageView) {

            }

            @Override
            public void getAction_barView_title(TextView textView) {
                textView.setText("json加密解密");
            }

            @Override
            public void getAction_barView_right_icon(ImageView imageView) {

            }
        });
        mTv_25show = (TextView) findViewById(R.id.tv_25showcallback);
        tv_25showhandler = (TextView) findViewById(R.id.tv_25showhandler);
        tv_clear = (TextView) findViewById(R.id.tv_clear);

        mLv_listview = (ListView) findViewById(R.id.lv_listview);
        mTv_25_getjson = (TextView) findViewById(R.id.tv_25_getjson);
        mTv_25show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Hashtable<String, String> hashMap = new Hashtable<>();
                //必要字段
                //"action", "getXinXi"
                //"passenger_telephone", "13664608180"
//                hashMap.put("action","getXinXi");
//                hashMap.put("passenger_telephone","13664608180");

                hashMap.put("action", "sendPosition");
                hashMap.put("driver_id", "2");
                hashMap.put("positon_lat", "45.746566");
                hashMap.put("positon_lon", "126.604894");
                hashMap.put("positon_name", "测试");
                hashMap.put("positon_passenger", "0");

                NetMessage.get(MainActivity_25_jsonjiamijiemi.this)
                        .sendMessage(Constants.new_url,hashMap, new NetAesCallBack() {
                            @Override
                            public void onSucceed(JSONObject jsonObject) {
                                try{
//                                    result_ data = jsonObject.getObject("data", result_.class);
//
//                                    ArrayList<result_> objects = new ArrayList<>();
//                                  //  mTv_25_getjson.setText(data.toString());
//                                    for (int i = 0; i < 20; i++) {
//                                        objects.add(data);
//                                    }
                                    Che data = jsonObject.toJavaObject(Che.class);
                                    List<Che.DataBean> data1 = data.data;

                                    initAdapter(data1);

                                }catch (Exception e){
                                    UtilsToast.showToast(MainActivity_25_jsonjiamijiemi.this, "json解析出错"+jsonObject.toString());
                                    e.printStackTrace();
                                }

                            }

                            @Override
                            public void onError(String errorString) {
                            LogUtils.i("errorString"+errorString);
                            }
                        });
//                NetMessage.get(MainActivity_25_jsonjiamijiemi.this)
//                        .sendMessage(hashMap, new NetAesCallBack() {
//                    @Override
//                    public void getAesString(String aesString) {
//                        LogUtils.i("加密内容"+aesString);
//                        Message message = mHandler.obtainMessage();
//                        //message.what = 1;
//                        Bundle bundle = new Bundle();
//                        bundle.putString("aesString",aesString);
//                        message.setData(bundle);
//                        mHandler.sendMessage(message);
//
//
//                        //mTv_25_getjson.setText(aesString);
//                       // LogUtils.i("内容是修改之后的"+mTv_25_getjson.getText().toString());
//                    }
//                });

            }
        });
        tv_25showhandler.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Hashtable<String, String> hashMap = new Hashtable<>();
                for (int i = 0; i < 10; i++) {
                    hashMap.put(""+i,""+i);
                }

            }
        });
        tv_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mTv_25_getjson.setText("");
            }
        });

        initData();
    }

    private void initAdapter(List<Che.DataBean> objects) {

        mmyAdapager mmyAdapager = new mmyAdapager(this, 0, objects, new BaseListViewAdapter_04.CallBack() {
            @Override
            public void loadMore(View view, int position, int type) {

            }
        });

        mLv_listview.setAdapter(mmyAdapager);
    }

    @Override
    public void initData() {

    }
}
